#ifndef __TASK_JOINT_HH_
#define __TASK_JOINT_HH_
#include <stdint.h>
#include <map>
#include <string>
#include <vector>

#define JOINT_NUM (7)  //TrajConfig.Joints

class ServoStatistic
{
public:
    struct ServoStatisticChn
    {
        uint32_t cntBeforePowerOn;  //上电前计数
        uint32_t cntWhenPowerOn;    //上电后的计数，与servo—can统计的数据一致
        uint32_t cntLastDay;        //前一天的个数，用于计算每日统计
        // uint32_t lastWarnCnt;       //上一次弹窗报警给app时刻的计数
    };

public:
    static ServoStatistic* get();
    ServoStatistic();
    ~ServoStatistic();
    int init(uint32_t serialNum = 0);
    void load(void* file);
    int reset(uint32_t serialNum);
    void write2File(void* file);
    int update(uint8_t joint, uint32_t ecode, uint32_t cnt);
    void updateDaliy();
    int check(bool report = true);
    uint32_t getAllCnt(uint8_t joint, uint32_t ecode);
    uint32_t getDaliyCnt(uint8_t joint, uint32_t ecode);
    inline uint32_t getDate() { return _date; }
    inline uint32_t getThreshold(uint8_t joint) { return _warnThresHold[joint]; }
    void printServoStatistic(int8_t index = -1);
    std::string formatDaliyWarnStr();
    inline void debug(int i)
    {
        for (auto e : _servoStatistic[i])
        { printf("[SS_D]J%d:%d %d %d %d\n", i, e.first, e.second.cntBeforePowerOn, e.second.cntWhenPowerOn, e.second.cntLastDay); }
    }

protected:
    void setStatistic(uint32_t errcode, std::vector<int>& cnt, std::vector<int>& cntlast);
    uint32_t isNewDay(uint32_t d);
    void formatStatistic(uint32_t ecode, std::string& all, std::string& daliy);

protected:
    uint32_t _date;
    std::map<uint32_t, ServoStatisticChn> _servoStatistic[JOINT_NUM];
    uint32_t _warnThresHold[JOINT_NUM];
    int _reportedDaliyWarn;  //bit为true表示当日已经上报过错误
    uint32_t _serialNum;
};
#endif